package com.tn.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.Claim;
import com.databstech.apis.annotation.eum.SginEnum;
import com.databstech.apis.annotation.mapping.ApiMapping;
import com.databstech.apis.annotation.mapping.PathRestController;
import com.databstech.apis.annotation.mapping.Signature;
import com.databstech.apis.result.enums.ResultCodeEnum;
import com.databstech.apis.result.response.ResultVO;
import com.databstech.apis.resultexception.exception.BusinessException;
import com.databstech.utils.jwt.util.JwtUtil;
import com.tn.user.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author tn
 * @version 1
 * @ClassName UserController
 * @description 登录
 * @date 2020/8/11 12:17
 */
//@RestController
//@RequestMapping("user")
@PathRestController("userInfo")
@Validated
@Api(tags = "user - user")
public class UserController {


    @ApiOperation("user - 登录")
    @GetMapping("login")
    public ResultVO login(@Valid UserVo userName) {
        if(!userName.getUserName().equals("tn")){
            return ResultVO.fail("登录失败");
        }
        String token = JwtUtil.sign(userName.getUserName(), null);
        return ResultVO.success(token);
    }


    @ApiOperation("user - testNotBlank")
    @GetMapping(value = "testNotBlank")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户名",type = "String",paramType = "query"),
            @ApiImplicitParam(name = "test",value = "test",type = "String",paramType = "query"),
    })
    public ResultVO login(@NotBlank(message ="userName不能为空") String userName, String test) {
        return ResultVO.success(userName);
    }

    @ApiOperation(value = "user - 验证登录")
    @GetMapping(value = "verityTokenForMap")
    public Object verityTokenForMap(String token) {
        return ResultVO.success(JwtUtil.verityForMap(token));
    }

    @ApiOperation(value = "user - 验证登录")
    @GetMapping(value = "verityToken")
    public ResultVO<String> verityToken(String token) {
        return ResultVO.resultMsg(JwtUtil.verity(token),"登录");
    }

    @ApiOperation(value = "user - 获取token中的数据")
    @GetMapping(value = "getTokenData")
    public ResultVO<Object> getTokenData(String token) {
        Map<String, Claim> stringClaimMap = JwtUtil.parseJWT(token);
        Iterator<String> iterator = stringClaimMap.keySet().iterator();
        while(iterator.hasNext()){
          String key = iterator.next();
            System.out.println("key:"+key);

            Claim claim = stringClaimMap.get(key);
            System.out.println("Claim:"+ JSONObject.toJSONString(claim));

            List[] lists = claim.asArray(List.class);
//            System.out.println("Claim:"+claim);
        }
        return ResultVO.success();
    }

    @ApiOperation(value = "user - Post")
    @PostMapping(value = "testPost")
    public ResultVO testPost() {
        return ResultVO.success("testPost");
    }

    @ApiOperation(value = "user - Exception")
    @PostMapping(value = "testException")
    public ResultVO testException() {
        throw new BusinessException(ResultCodeEnum.SysAuthorizedPast,"测试异常");
    }

    @ApiOperation(value = "user - ApiMapping")
    @ApiMapping(value = "testApiMapping")
    public ResultVO testApiMapping() {
        return ResultVO.success("testApiMapping");
    }


    @ApiOperation(value = "user - sign")
    @ApiMapping(value = "testSign")
    @Signature(type = SginEnum.MD5)
    public ResultVO testSign(String name,String sign) {
        return ResultVO.success("testSign");
    }

}
